package com.example.xiaozu.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import com.example.xiaozu.R;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;

public class SettingsActivity extends AppCompatActivity {
    private SwitchCompat switchAdminMode;
    private LinearLayout layoutAddArticle, layoutAddProduct;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        // 初始化视图
        switchAdminMode = findViewById(R.id.switch_admin_mode);
        layoutAddArticle = findViewById(R.id.layout_add_article);
        layoutAddProduct = findViewById(R.id.layout_add_product);

        // 账号管理
        findViewById(R.id.layout_account_manage).setOnClickListener(v -> {
            startActivity(new Intent(this, AccountManageActivity.class));
        });

        // 退出登录
        findViewById(R.id.layout_logout).setOnClickListener(v -> {
            // 清除登录状态
            getSharedPreferences("user_prefs", MODE_PRIVATE).edit().clear().apply();
            startActivity(new Intent(this, LoginActivity.class));
            finishAffinity();
        });

        // 管理员模式切换
        switchAdminMode.setOnCheckedChangeListener((buttonView, isChecked) -> {
            updateAdminUIVisibility(isChecked);
            // 保存管理员状态
            getSharedPreferences("app_prefs", MODE_PRIVATE).edit()
                    .putBoolean("admin_mode", isChecked).apply();
        });

        // 初始化管理员状态
        boolean isAdmin = getSharedPreferences("app_prefs", MODE_PRIVATE)
                .getBoolean("admin_mode", false);
        switchAdminMode.setChecked(isAdmin);
        updateAdminUIVisibility(isAdmin);

        // 添加文章
        layoutAddArticle.setOnClickListener(v -> {
            startActivity(new Intent(SettingsActivity.this, AddArticleActivity.class));
        });

// 添加商品
        layoutAddProduct.setOnClickListener(v -> {
            startActivity(new Intent(SettingsActivity.this, AddProductActivity.class));
        });
    }

    private void updateAdminUIVisibility(boolean isAdmin) {
        layoutAddArticle.setVisibility(isAdmin ? View.VISIBLE : View.GONE);
        layoutAddProduct.setVisibility(isAdmin ? View.VISIBLE : View.GONE);
    }
}

